package com.teacher.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.teacher.common.CommonResult;
import com.teacher.mapper.TermMapper;
import com.teacher.pojo.dto.aestheticeducationbasic.AestheticEducationBasicGetDto;
import com.teacher.pojo.entity.AestheticEducationBasic;
import com.teacher.mapper.AestheticEducationBasicMapper;
import com.teacher.pojo.entity.MoralEducationBasic;
import com.teacher.pojo.entity.PerStudent;
import com.teacher.pojo.vo.aestheticeducationbasic.AestheticEducationBasicGetVo;
import com.teacher.pojo.vo.moraleducationbaisc.MoralEducationBasicGetVo;
import com.teacher.service.IAestheticEducationBasicService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.teacher.service.IPerStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 美育基本分 服务实现类
 * </p>
 *
 * @author zfm
 * @since 2021-08-13
 */
@Service
public class AestheticEducationBasicServiceImpl extends ServiceImpl<AestheticEducationBasicMapper, AestheticEducationBasic> implements IAestheticEducationBasicService {

    @Autowired
    private IPerStudentService iPerStudentService;

    @Autowired
    private TermMapper termMapper;

    /**
     * 初始化美育基本分
     *
     * @param classId
     * @return
     */
    @Override
    public CommonResult<String> init(Long classId) {

        List<PerStudent> studentList = iPerStudentService.list(new QueryWrapper<PerStudent>().lambda()
                .eq(PerStudent::getClassId, classId));
        List<AestheticEducationBasic> basicList = new ArrayList<>();
        Long currentTermId = termMapper.getCurrentTerm();
        List<String> studentKeyList = studentList.stream().map(PerStudent::getStudentKey).collect(Collectors.toList());

        if (studentList.size() > 0) {
            // 删除原有的数据
            this.remove(new QueryWrapper<AestheticEducationBasic>().lambda()
                    .eq(AestheticEducationBasic::getTermId, currentTermId)
                    .in(AestheticEducationBasic::getStudentKey, studentKeyList));
            for (PerStudent perStudent : studentList) {
                AestheticEducationBasic educationBasic = new AestheticEducationBasic();
                educationBasic.setTermId(currentTermId);
                educationBasic.setArtShow(true);
                educationBasic.setDoActivity(true);
                educationBasic.setSeeMove(true);
                educationBasic.setStudentKey(perStudent.getStudentKey());
                basicList.add(educationBasic);
            }
            if (basicList.size() > 0) {
                this.saveBatch(basicList);
            }
        }
        return CommonResult.success("初始化成功");
    }

    /**
     * 查询美育基本分
     *
     * @param dto
     * @return
     */
    @Override
    public CommonResult<IPage<AestheticEducationBasicGetVo>> getPage(AestheticEducationBasicGetDto dto) {
        Page<AestheticEducationBasicGetVo> page = new Page<>(dto.getCurrentPage(), dto.getPageSize());
        if (dto.getTermId() == null) {
            dto.setTermId(termMapper.getCurrentTerm());
        }
        IPage<AestheticEducationBasicGetVo> iPage = baseMapper.getPage(page, dto);
        return CommonResult.success(iPage);
    }
}
